<script setup>
import PageContainer from './components/PageContainer.vue'
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { artGetListService, artDeleteService } from '@/api/article'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'
const total = ref(0)
//表单
const formModel = ref()
const loading = ref(false)
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
const onSearch = async () => {
  loading.value = true
  params.value.pagenum = 1
  await getArticleList()
  loading.value = false
}

const onReset = async () => {
  loading.value = true
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  await getArticleList()
  loading.value = false
}
//表格
const articleList = ref([])
const getArticleList = async () => {
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
}
getArticleList()
//抽屉

const artEdit = ref()
const onSuccess = (str) => {
  loading.value = true
  console.log(str)
  if (str === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    console.log(lastPage)
    params.value.pagenum = lastPage
  }
  getArticleList()
  loading.value = false
}
//分页
const onEdit = (row) => {
  artEdit.value.open(row)
  console.log('row', row)
}
const onPublish = () => {
  artEdit.value.open({})
}
const onSizeChange = async (size) => {
  //size更新是需要回到第一页
  loading.value = true
  params.value.pagenum = 1
  params.value.pagesize = size
  await getArticleList()
  loading.value = false
}
const onCurrentChange = async (page) => {
  loading.value = true
  params.value.pagenum = page
  await getArticleList()
  loading.value = false
}
const onDelete = async (row) => {
  loading.value = true
  await artDeleteService(row.id)
  await getArticleList()
  loading.value = false
}
</script>
<template>
  <PageContainer title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onPublish()">发布文章</el-button>
    </template>
    <template #default>
      <el-form :inline="true" ref="formModel" :model="form">
        <el-form-item label="文章分类：">
          <ChannelSelect v-model="params.cate_id"></ChannelSelect>
        </el-form-item>
        <el-form-item label="发布状态：">
          <el-select v-model="params.state">
            <el-option label="已发布" value="已发布" />
            <el-option label="草稿" value="草稿" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="onSearch" type="primary">搜索</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table style="width: 100%" :data="articleList" v-loading="loading">
        <el-table-column prop="title" label="文章标题" width="180" />
        <el-table-column prop="cate_name" label="分类" width="180" />
        <el-table-column prop="pub_date" label="发表时间">
          <template #default="{ row }">
            {{ formatTime(row.pub_date) }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              :icon="Edit"
              circle
              type="primary"
              plain
              @click="onEdit(row)"
            >
            </el-button>
            <el-button
              :icon="Delete"
              circle
              type="danger"
              plain
              @click="onDelete(row)"
            >
            </el-button>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="没有数据"></el-empty>
        </template>
      </el-table>
      <ArticleEdit
        ref="artEdit"
        @success="onSuccess($event, str)"
      ></ArticleEdit>
      <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 3, 4, 5, 10]"
        layout="jumper, total, sizes, prev, pager, next"
        background
        :total="total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
        style="margin-top: 20px; justify-content: flex-end"
      />
    </template>
  </PageContainer>
</template>

<style></style>
